package com.supply.dl.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.supply.dl.R;
import com.supply.dl.global.Global;
import com.supply.dl.utils.StringUtils;
import com.supply.dl.utils.utils.ToastUtil;

import butterknife.BindView;

/**
 * Created by ${qianzhihe} on 2018/7/9.
 * 设置IP地址和端口
 * 临时使用的测试界面
 */

public class SetIpAndPortActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener{


    @BindView(R.id.set_ip_and_port_main_editIp)
    EditText edtIp;

    @BindView(R.id.set_ip_and_port_main_editPort)
    EditText edtPort;

    @BindView(R.id.set_ip_and_port_main_tvSure)
    TextView tvSure;

    @BindView(R.id.set_ip_and_port_main_rg)
    RadioGroup radioGroup;

    @BindView(R.id.set_ip_and_port_main_rbHttp)
    RadioButton rbHttp;

    @BindView(R.id.set_ip_and_port_main_rbHttps)
    RadioButton rbHttps;

    String ipString="";
    String portString="";
    String httpString="";

    boolean isHTTPRequest;

    @Override
    protected int getLayoutResource() {
        return R.layout.set_ip_and_port_main;
    }

    @Override
    protected void initView() {
        super.initView();
        showLeftBack();
        setRightInVisible();
        setTitleShow("设置HTTP地址");
    }

    @Override
    protected void initListener() {
        super.initListener();
        tvSure.setOnClickListener(this);
        radioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        isHTTPRequest=true;
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.set_ip_and_port_main_tvSure:
                ipString=edtIp.getText().toString();
                portString=edtPort.getText().toString();
                if (StringUtils.isEmpty(ipString)){
                    ToastUtil.showToast(this, "IP地址输入不能能空！");
                    return;
                }
                if (isHTTPRequest){
                    httpString+="http://";
                }else {
                    httpString+="https://";
                }
                if (!StringUtils.isEmpty(portString)){
                    httpString+=ipString+":"+portString+"/";
                }else {
                    httpString+=ipString+"/";
                }
                Global.getInstance().setHTTPAddress(httpString);
                finish();
                break;

        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId==R.id.set_ip_and_port_main_rbHttp){
            isHTTPRequest=true;
        }else if (checkedId==R.id.set_ip_and_port_main_rbHttps){
            isHTTPRequest=false;
        }
    }
}
